package com.soft.app.ruanba.down;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import org.xml.sax.helpers.DefaultHandler;

public class FileDownloadHandler extends DefaultHandler {
	private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(FileDownloadHandler.class);
	private java.util.Stack tags;
	private static int i = 1;
	private FileVo fileVo;
	private List<FileVo> files;

	public FileDownloadHandler()
	{
		tags = new Stack();
		files = new ArrayList<FileVo>();
		fileVo = null;
	}

	public void startDocument()
		throws org.xml.sax.SAXException
	{
		log.info("开始解析");
	}

	public void characters(char ch[], int start, int length)
		throws org.xml.sax.SAXException
	{
		java.lang.String tag = (java.lang.String)tags.peek();
		java.lang.String value = new String(ch, start, length);
		if (tag.equals("filename"))
			fileVo.setFileName(value);
		else
		if (tag.equals("type"))
			fileVo.setFileType(value);
		else
		if (tag.equals("download"))
		{
			fileVo.setUrl(value);
		}
	}

	public List<FileVo> getFiles() {
		return files;
	}

	public void setFiles(List<FileVo> files) {
		this.files = files;
	}

	public void endDocument()
		throws org.xml.sax.SAXException
	{
		log.info("成功结束任务!");
	}

	public void endElement(java.lang.String uri, java.lang.String localName, java.lang.String name)
		throws org.xml.sax.SAXException
	{
		java.lang.String popName = (java.lang.String)tags.pop();
		if (name.equals("soft"))
			files.add(fileVo);
	}

	public void startElement(java.lang.String uri, java.lang.String localName, java.lang.String name, org.xml.sax.Attributes attributes)
		throws org.xml.sax.SAXException
	{
		tags.push(name);
		if (name.equals("soft"))
		{
			fileVo = new FileVo();
		}
	}
}
